package com.atguigu.flink.datastream.transform;

import com.alibaba.fastjson.JSON;
import com.atguigu.flink.func.ClickSource;
import com.atguigu.flink.pojo.Event;
import com.atguigu.flink.pojo.Wordcount;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @author WEIYUNHUI
 * @date 2023/6/13 14:06
 * <p>
 * 聚合算子:
 * 简单聚合  :  sum  min  minBy  max  maxBy
 * 归约聚合  :  reduce
 * <p>
 * Flink要求:  要聚合，先keyBy
 */
public class Flink02_AggTransform {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<Event> ds = env.addSource(new ClickSource());
        ds.print("input");

        //需求: 统计每个user的点击次数
        ds.map(
                        event -> new Wordcount(event.getUser(), 1)
                ).returns(Wordcount.class)
                .keyBy(
                        Wordcount::getWord
                )
                .sum("count")
        //.print("count");
        ;


        //需求: 求每个user最后点击的数据
        /**
         * max结果：
         *
         * input> Event(user=Peiqi, url=pay, ts=1686661626703)
         * max> Event(user=Peiqi, url=pay, ts=1686661626703)
         * input> Event(user=Peiqi, url=/cart, ts=1686661631709)
         * max> Event(user=Peiqi, url=pay, ts=1686661631709)
         * input> Event(user=Peiqi, url=/cart, ts=1686661637712)
         * max> Event(user=Peiqi, url=pay, ts=1686661637712)
         *
         * maxBy结果：
         *
         * input> Event(user=Peiqi, url=pay, ts=1686661858856)
         * max> Event(user=Peiqi, url=pay, ts=1686661858856)
         * input> Event(user=Peiqi, url=/home, ts=1686661863857)
         * max> Event(user=Peiqi, url=/home, ts=1686661863857)
         * input> Event(user=Peiqi, url=/home, ts=1686661867859)
         * max> Event(user=Peiqi, url=/home, ts=1686661867859)
         */
        ds.keyBy(
                        Event::getUser
                )
//                .max("ts")
                .maxBy("ts")
                .print("max");


        try {
            env.execute();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
